package com.evildog.workspace.system.application.controller;

import com.evildog.workspace.common.result.Result;
import com.evildog.workspace.common.result.ResultGenerator;
import com.evildog.workspace.dependencies.mybatis.object.BaseConvert;
import com.evildog.workspace.dependencies.mybatis.object.BaseStatusParam;
import com.evildog.workspace.system.application.bo.MenuPermissionBO;
import com.evildog.workspace.system.application.bo.MenuPermissionTreeBO;
import com.evildog.workspace.system.application.convert.MenuPermissionConvert;
import com.evildog.workspace.system.application.model.MenuPermissionCreateParam;
import com.evildog.workspace.system.application.model.MenuPermissionUpdateApiParam;
import com.evildog.workspace.system.application.model.MenuPermissionUpdateParam;
import com.evildog.workspace.system.application.service.MenuPermissionService;
import com.evildog.workspace.system.application.vo.MenuPermissionTreeVO;
import com.evildog.workspace.system.application.vo.MenuPermissionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * menuPermission Controller
 * 菜单
 * </p>
 *
 * @author evildog
 * @since 2020-07-25
 */
@RestController
@RequestMapping("/menu-permissions")
@AllArgsConstructor
@Api(tags = "menuPermission相关接口")
public class MenuPermissionController {

    private MenuPermissionService menuPermissionService;

    @PostMapping
    @ApiOperation(value = "添加接口")
    public Result<MenuPermissionVO> save(@Validated @RequestBody MenuPermissionCreateParam menuPermissionCreateParam) {
        MenuPermissionBO menuPermissionBO = menuPermissionService.save(MenuPermissionConvert.INSTANCE.convert(menuPermissionCreateParam));
        return ResultGenerator.success(MenuPermissionConvert.INSTANCE.convert(menuPermissionBO));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public Result<Boolean> removeById(@PathVariable("id") Long id) {
        return ResultGenerator.status(menuPermissionService.remove(id));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public Result<MenuPermissionVO> getById(@PathVariable("id") Long id) {
        return ResultGenerator.success(MenuPermissionConvert.INSTANCE.convert(menuPermissionService.getDetailById(id)));
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "更新接口")
    public Result<Boolean> updateById(@PathVariable("id") Long id, @Validated @RequestBody MenuPermissionUpdateParam param) {
        return ResultGenerator.status(menuPermissionService.updateById(id, MenuPermissionConvert.INSTANCE.convert(param)));
    }

    @PatchMapping("/{id}/status")
    @ApiOperation(value = "更新status接口")
    public Result<Boolean> updateStatus(@PathVariable("id") Long id, @Validated @RequestBody BaseStatusParam roleParam) {
        return ResultGenerator.status(menuPermissionService.updateStatus(id, BaseConvert.INSTANCE.convert(roleParam)));
    }

    /**
     * 查询所有权限
     */
    @GetMapping("/tree")
    @ApiOperation(value = "tree menu数据")
    public Result<List<MenuPermissionTreeVO>> getList() {
        List<MenuPermissionTreeBO> menuPermissionTreeBO = menuPermissionService.getTree();
        return ResultGenerator.success(MenuPermissionConvert.INSTANCE.convertVoList(menuPermissionTreeBO));
    }

    /**
     * 修改菜单关联的api 权限
     */
    @PostMapping("/{id}/api-permissions")
    public Result<Boolean> updateApiPermissions(@PathVariable("id") Long id, @RequestBody MenuPermissionUpdateApiParam param) {
        return ResultGenerator.status(menuPermissionService.updateApiPermissions(id, MenuPermissionConvert.INSTANCE.convert(param)));
    }

}